package com.xiangyu.demo.controller;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.xiangyu.demo.bean.ServerMessage;
import com.xiangyu.demo.bean.UserInfo;
import com.xiangyu.demo.service.DemoService;
import com.xiangyu.demo.configuration.ConnectionProperties;
import com.xiangyu.demo.utils.ResultOb;
import com.xiangyu.demo.utils.VerifyHandler;
import com.xiangyu.demo.service.WebSocketService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * java类简单作用描述
 *
 * @ProjectName: springboot-demo
 * @Package: com.xiangyu.demo.controller
 * @Description: java类作用描述
 * @Author: 向羽
 * @CreateDate: 2018/1/10 17:23
 * @UpdateUser: 向羽
 * @UpdateDate: 2018/1/10 17:23
 * @UpdateRemark: The modified content
 * @Version: 2.0
 */

@RestController
public class DemoController {
    private Logger logger = LoggerFactory.getLogger(DemoController.class);
    @Autowired
    private DemoService demoService;
    @Autowired
    private ConnectionProperties connectionProperties;
    @Value("${com.xiangyu.blog.face_api_key}")
    private String API_KEY;
    @Value("${com.xiangyu.blog.number}")
    private Integer number;
    @Value("${com.xiangyu.blog.number2}")
    private Integer number2;
    @Autowired
    private SimpMessagingTemplate messagingTemplate;


    @RequestMapping("/user")
    @VerifyHandler(isAuth = true)
    public List<UserInfo> index(UserInfo userInfo){
        PageBounds pageBounds = new PageBounds(1,10);
        List<UserInfo> str = demoService.getHello(userInfo,pageBounds);
        String userName = connectionProperties.getUsername();
        //发送消息给指定用户
        for (UserInfo u: str) {
            messagingTemplate.convertAndSendToUser("test", "/queue/message/"+u.getUserId(), new ServerMessage("服务器主动推的数据"));
        }
        messagingTemplate.convertAndSendToUser("test", "/queue/message", new ServerMessage("服务器主动推的数据"));
        logger.debug("userName==>"+userName);
        logger.debug("MY_SECRET==>"+number);
        logger.debug("MY_SECRET==>"+number2);
        return str;
    }

    @RequestMapping("/user2")
    @VerifyHandler(isAuth = false)
    public List<UserInfo> index2(UserInfo userInfo){
        List<UserInfo> str = demoService.getHello2(userInfo);
        String userName = connectionProperties.getUsername();
        logger.debug("userName==>"+userName);
        logger.debug("MY_SECRET==>"+number);
        logger.debug("MY_SECRET==>"+number2);
        return str;
    }

    @RequestMapping("/delUser")
    @VerifyHandler(isAuth = false)
    public ResultOb delUser(UserInfo userInfo){

        ResultOb resultOb = new ResultOb();
        resultOb.setData(demoService.delUser(userInfo));

        return resultOb;
    }

}
